﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class ChiTietHoaDonDao
    {
        public static ChiTietHoaDonDto layChiTietDonHang(int id)
        {
            ChiTietHoaDonDto result = null;
            try
            {
                // tao ket noi CSDL 
                SqlConnection sqlconn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = new SqlCommand("sp_layChiTietDonHang", sqlconn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@ID", System.Data.SqlDbType.Int);
                cmd.Parameters["@ID"].Value = id;

                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result = new ChiTietHoaDonDto();

                    result.IDHoaDon = (int)dr["IDhoadon"];
                    result.IDSanPham = (int)dr["IDsanpham"];
                    result.SoLuong = (int)dr["soluong"];
                }

                // dong ket noi
                sqlconn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return result;
        }
        public static List<ChiTietHoaDonDto> LayDanhSachChiTietHoaDonTheoIDSanPham(int idsanpham)
        {
            List<ChiTietHoaDonDto> ds = null;
            try
            {
                // tao ket noi CSDL 
               SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                string sql = "Select * From ChiTietHoaDon where IDsanpham=" + idsanpham;
                SqlCommand cmd = new SqlCommand(sql, cnn);
                SqlDataReader dr = cmd.ExecuteReader();
                ds = new List<ChiTietHoaDonDto>();
                ChiTietHoaDonDto result = null;
                while (dr.Read())
                {
                    result = new ChiTietHoaDonDto();

                    result.IDHoaDon = (int)dr["IDhoadon"];
                    result.IDSanPham = (int)dr["IDsanpham"];
                    result.SoLuong = (int)dr["soluong"];
                    ds.Add(result);
                }

                // dong ket noi
                cnn.Close();
            
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return ds;
        }
        public static bool XoaChiTietHoaDonTheoPK(int idhoadon, int idsanpham)
        {
            try
            {
                HoaDonDto hoadon = HoaDonDao.LayHoaDonTheoID(idhoadon);
                if (hoadon.TinhTrang != 1)
                    return false;
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                string sql = "Delete From ChiTietHoaDon where IDsanpham=" + idsanpham+ " and IDhoadon="+idhoadon;
                SqlCommand cmd = new SqlCommand(sql, cnn);
                int i = cmd.ExecuteNonQuery();
                if(i==1)
                    return true;
                
                return false;
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }

        public static bool XoaChiTietHoaDonTheoIDSanPham(int idsanpham)
        {

            List<ChiTietHoaDonDto> ds = LayDanhSachChiTietHoaDonTheoIDSanPham(idsanpham);
            foreach (ChiTietHoaDonDto ct in ds)
            {
                HoaDonDto hoadon = HoaDonDao.LayHoaDonTheoID(ct.IDHoaDon);
                if (hoadon.TinhTrang != 1)
                    return false;    
            }
            bool res;
            foreach (ChiTietHoaDonDto ct in ds)
            {
                res = XoaChiTietHoaDonTheoPK(ct.IDHoaDon, ct.IDSanPham);
                if (res == false)
                    return false;
                HoaDonDao.XoaHoaDon(ct.IDHoaDon);
            }
            return true;
        }

        public static bool ThemChiTietHoaDon(GioHangDto GioHang, int IDHoaDon)
        {
            try
            {
                SqlConnection cnn = DataConnector.MakeConnection();
                string sql;
                int Err;
                SqlCommand cmd ;
                for (int i = 0; i < GioHang.lstHang.Count; i++)
                {
                    sql = "Insert into ChiTietHoaDon(IDHoaDon, IDSanPham, SoLuong) values(" + IDHoaDon + ", " + GioHang.lstHang[i].ID + ", " + GioHang.lstHang[i].SoLuong + ")";
                    cmd = new SqlCommand(sql, cnn);
                    Err = cmd.ExecuteNonQuery();
                    // Neu that bai trong viec them 1 chi tiet hoa don se xoa nhung chi tiet da duoc insert thanh cong
                    if (Err != 1)
                    {
                        XoaChiTietHoaDonTheoIDSanPham(IDHoaDon);
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }

        public static List<DTO.ChiTietHoaDonDto> LayChiTietHoaDonTheoIDHoaDon(int idHoaDon)
        {
            SqlConnection cnn = DataConnector.MakeConnection();

            string sql = "Select * From ChiTietHoaDon Where IDHoaDon=" + idHoaDon;
            SqlCommand cmd = new SqlCommand(sql, cnn);
            SqlDataReader dr = cmd.ExecuteReader();

            List<ChiTietHoaDonDto>  ds = new List<ChiTietHoaDonDto>();
            ChiTietHoaDonDto result = null;
            while (dr.Read())
            {
                result = new ChiTietHoaDonDto();

                result.IDHoaDon = (int)dr["IDhoadon"];
                result.IDSanPham = (int)dr["IDsanpham"];
                result.SoLuong = (int)dr["soluong"];
                ds.Add(result);
            }

            // dong ket noi
            cnn.Close();
            return ds;
        }

        public static bool XoaChiTietHoaDonTheoIDHoaDon(int idHoaDon)
        {

            List<ChiTietHoaDonDto> ds = LayChiTietHoaDonTheoIDHoaDon(idHoaDon);
            
            bool res;
            foreach (ChiTietHoaDonDto ct in ds)
            {
                res = XoaChiTietHoaDonTheoPK(ct.IDHoaDon, ct.IDSanPham);
                if (res == false)
                    return false;
            }
            return true;
        }
    }
}
